package com.pv.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pv.user.service.SmsService;
import com.pv.user.vo.Rooftop;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pv.user.entity.RooftopEntity;
import com.pv.user.service.RooftopService;
import com.pv.common.utils.PageUtils;
import com.pv.common.utils.R;
import com.pv.common.entity.PageRequest;



/**
 * 屋顶表
 *
 * @author zk
 * @email 2638561747@qq.com
 * @date 2021-08-26 09:24:56
 */
@RestController
@RequestMapping("user/rooftop")
public class RooftopController {
    @Autowired
    private RooftopService rooftopService;

    @Autowired
    private SmsService smsService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation(value="查询全部",httpMethod="POST")
    public R list(@RequestBody PageRequest pageRequest){
        PageUtils page = rooftopService.queryPage(pageRequest);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ApiOperation(value="根据主键查询", notes="参数只用到了主键." ,httpMethod="POST")
    public R info(@PathVariable("id") Integer id){
		RooftopEntity rooftop = rooftopService.getById(id);
        return R.ok().put("rooftop", rooftop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation(value="添加", notes="" ,httpMethod="POST")
    public R save(@RequestBody Rooftop.applyVo rooftop){
        if (StringUtils.isEmpty(rooftop.getPhone())){
            return R.error("手机号不能为空");
        }
        String phone = rooftop.getPhone();
        RooftopEntity one = rooftopService.getOne(new LambdaQueryWrapper<RooftopEntity>().eq(RooftopEntity::getPhone, phone));
        if (one!=null){
            return R.error("手机号不能重复申请");
        }
        if (!smsService.matcheCodeAndGetPhone(phone,rooftop.getCode())){
            return R.error("验证码错误");
        }else {
            //TODO 邀请码未处理
            RooftopEntity rooftopEntity = new RooftopEntity();
            BeanUtils.copyProperties(rooftop,rooftopEntity);
            rooftopService.save(rooftopEntity);
            return R.ok();
        }
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @ApiOperation(value="修改", notes="" ,httpMethod="POST")
    public R update(@RequestBody RooftopEntity rooftop){
		rooftopService.updateById(rooftop);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ApiOperation(value="删除", notes="" ,httpMethod="POST")
    public R delete(@RequestBody Integer[] ids){
		rooftopService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
